<style>
    .layui-btn:not(.layui-btn-lg ):not(.layui-btn-sm):not(.layui-btn-xs) {height:34px;line-height:34px;padding:0 8px;}
</style>
<body>
<div class="layuimini-container layuimini-page-anim">
    <div class="layuimini-main">
        <div>
            <div class="layui-btn-group">
                <button class="layui-btn" id="btn-expand">全部展开</button>
                <button class="layui-btn layui-btn-normal" id="btn-fold">全部折叠</button>
            </div>

            <script type="text/html" id="toolbarDemo">
                <div class="layui-btn-container">
                    <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add"> 添加 </button>
                </div>
            </script>

            <table id="munu-table" class="layui-table" lay-filter="munu-table"></table>
        </div>
    </div>
</div>
<!-- 操作列 -->
<script type="text/html" id="auth-state">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">修改</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>
<script>
    layui.use(['table', 'treetable'], function () {
        var $ = layui.jquery;
        var table = layui.table;
        var treetable = layui.treetable;
        var miniPage = layui.miniPage;
        var layer = layui.layer

        // 渲染表格
        layer.load(2);
        treetable.render({
            treeColIndex: 1,
            toolbar: '#toolbarDemo',
            treeSpid: 0,
            treeIdName: 'Id',
            treePidName: 'Pid',
            elem: '#munu-table',
            url: '/sys/MenuList',
            page: false,
            cols: [[
                {type: 'numbers'},
                {field: 'AuthName', minWidth: 200, title: '权限名称'},
                {field: 'UrlFor', title: '菜单url'},
                {field: 'Weight', width: 80, align: 'center', title: '权重'},
                {templet: '#auth-state', width: 120, align: 'center', title: '操作'}
            ]],
            done: function () {
                layer.closeAll('loading');
            }
        });

        $('#btn-expand').click(function () {
            treetable.expandAll('#munu-table');
        });

        $('#btn-fold').click(function () {
            treetable.foldAll('#munu-table');
        });

        /**
         * toolbar事件监听
         */
        table.on('toolbar(munu-table)', function (obj) {
            if (obj.event === 'add') {   // 监听添加操作
                var content = miniPage.getHrefContent('/sys/addMenu');
                var openWH = miniPage.getOpenWidthHeight();

                var index = layer.open({
                    title: '添加菜单',
                    type: 1,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: [openWH[0] + 'px', openWH[1] + 'px'],
                    offset: [openWH[2] + 'px', openWH[3] + 'px'],
                    content: content,
                    end:function() {
                       parent.location.reload();
                    }
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
            }
        });

        //监听工具条
        table.on('tool(munu-table)', function (obj) {
            var data = obj.data;
            var layEvent = obj.event;

            if (layEvent === 'del') {
                layer.confirm('真的删除行么', function (index) {
                    // FormData 对象
                    var form_data = new FormData();
                    form_data.append("id", data.Id);
                    delAction(form_data)
                });
            } else if (layEvent === 'edit') {
                var content = miniPage.getHrefContent('/sys/EditMenu?id=' + data.Id);
                var openWH = miniPage.getOpenWidthHeight();

                var index = layer.open({
                    title: '编辑菜单',
                    type: 1,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: [openWH[0] + 'px', openWH[1] + 'px'],
                    offset: [openWH[2] + 'px', openWH[3] + 'px'],
                    content: content,
                    end:function() {
                        parent.location.reload();
                    }
                });
            }
        });

        //删除广告
        function delAction(data){
            ajaxForm("/sys/DelMenu",data,function(res){
                layer.msg(res["Msg"], function () {
                    layer.closeAll()
                    parent.location.reload();
                });
            },function(res){
                layer.msg(res["Msg"]);
            })
        }
    });
</script>
</body>
</html>